// import Vue from 'vue';
import axios from 'axios';
// import store from '@/store'
import { Toast } from 'vant';
// import { ACCESS_TOKEN } from '@/store/mutation-types';
import VueAxios from './axios';

// 创建 axios 实例
const service = axios.create({
  baseURL: process.env.VUE_APP_SERVER_URL, // api base_url
  timeout: 10000, // 请求超时时间
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
});

const err = (error) => {
  if (error.response) {
    const { data } = error.response;
    // const token = Vue.ls.get(ACCESS_TOKEN)
    if (error.response.status === 403) {
      Toast.error(data.message);
    }
    if (error.response.status === 401) {
      Toast('Authorization verification failed');
    }
  }

  return Promise.reject(error);
};

// request interceptor
service.interceptors.request.use((Config) => {
  const config = Config;
  // const token = Vue.ls.get(ACCESS_TOKEN);
  // if (token) {
  //   config.headers.Authorization = token; // 如果有 让每个请求携带自定义 token 请根据实际情况自行修改
  // }
  return config;
}, err);

// response interceptor
service.interceptors.response.use((response) => response.data, err);

const installer = {
  vm: {},
  // eslint-disable-next-line no-shadow
  install(Vue) {
    Vue.use(VueAxios, service);
  }
};

export {
  installer as VueAxios,
  service as axios
};
